<template>
  <div>
    <el-row>
      <el-col :span="18" :offset="3"><div>
        <el-row type="flex" align="middle"  justify="left" class="row-bg" style="height: 70px;">
          <el-form :model="check" :rules="rules" :inline="true"  ref="check" status-icon class="demo-form-inline" label-position="left" size="small">
            <el-form-item label="商品名称" prop="goodName" style="margin-left: 12px">
              <el-input v-model="check.goodName" placeholder="请输入商品名称"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="queryGood">查询</el-button>
              <el-button type="primary" @click="doReset('check')">重置</el-button>
            </el-form-item>
            <el-form-item label="您当前的积分余额:">
              <span><font color="#00bfff" ><i>{{this.customerInfo.point}}</i></font></span>
            </el-form-item>
          </el-form>
        </el-row>
        <el-row>
          <el-col :span="24" style="margin-top: 1%">
            <div class="grid-content bg-purple-dark">
              <el-table
                  :data="pageGoodData.list"
                  height="450"
                  border
                  :header-cell-style="{'text-align':'center'}"
                  style="width: 100%">
                <el-table-column
                    prop="id"
                    label="id" v-if="false"
                >
                </el-table-column>
                <el-table-column
                    prop="goodCode"
                    label="商品编号"
                    width="120" align="center">
                </el-table-column>
                <el-table-column
                    label="商品图片"
                    width="208" align="center">
                  <template slot-scope="scope">
                    <el-avatar shape="square" :size="150" fit="fill" :src="scope.row.goodImg" ></el-avatar>
                  </template>
                </el-table-column>
                <el-table-column
                    prop="goodName"
                    label="商品名称"
                    width="200" align="center">
                </el-table-column>
                <el-table-column
                    prop="typeName"
                    label="商品类型"
                    width="133" align="center">
                </el-table-column>
                <el-table-column
                    prop="pointPrice"
                    label="兑换积分"
                    width="180" align="center">
                </el-table-column>
                <el-table-column
                    prop="repertory"
                    label="库存"
                    width="120" align="center">
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="240px" align="center">
                  <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark" content="兑换" placement="top-start">
                      <el-link type="primary" @click="goOrderPage(scope.row)">兑换</el-link>
                    </el-tooltip>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-col>
        </el-row>
        <!--    分页部分-->
        <el-row  id ="pagehelper" type="flex" justify="center" class="el-row">
          <el-col :span="24">
            <el-pagination
                background
                layout="prev,sizes, pager, next,total,jumper"
                :page-sizes="[1, 5, 10]"
                @next-click="nextPage"
                @prev-click="prevPage"
                @size-change="sizeChange"
                @current-change="currentPage"

                :page-size="pageGoodData.pageSize"
                :current-page="pageGoodData.pageNum"
                :total="pageGoodData.total"
                :page-count="pageGoodData.pages">
            </el-pagination>
          </el-col>
        </el-row>
      </div></el-col>
    </el-row>


<!--    确认订单页面-->
    <el-dialog title="确认订单" :visible.sync="dialogConfirmOrderVisible" width="35%">
      <el-form ref="form" :model="countPointMesssage"  label-width="80px">
        <el-form-item label="商品名称:" style="margin-left:25%">
          <span>{{goodOrderInfo.goodName}}</span>
        </el-form-item>
        <el-form-item
            label="商品图片"
            width="150" align="center" style="margin-left:25%" >
            <el-avatar shape="square" :size="100" fit="fill" :src="goodOrderInfo.goodImg" ></el-avatar>
        </el-form-item>
        <el-form-item label="商品数量:" style="margin-left:25%">
          <el-input-number v-model="countPointMesssage.orders.buyNum"  :min="1" :max="19" label="商品数量"></el-input-number>
        </el-form-item>
        <el-form-item label="收货地区:" style="margin-left:25%">
          <el-input v-model="countPointMesssage.address.region" placeholder="请输入收货地址所在省份" style="width: 280px"></el-input>
        </el-form-item>
        <el-form-item label="收货地址:" style="margin-left:25%">
          <el-input v-model="countPointMesssage.address.address" placeholder="请输入收货地址" style="width: 280px"></el-input>
        </el-form-item>
        <el-form-item label="收货人:" style="margin-left:25%">
          <el-input v-model="countPointMesssage.address.receiver" placeholder="请输入收货人" style="width: 280px"></el-input>
        </el-form-item>
        <el-form-item label="收货人联系方式:" style="margin-left:25%">
          <el-input v-model="countPointMesssage.address.phone" placeholder="请输入收货人电话" style="width: 280px"></el-input>
        </el-form-item>
        <el-form-item label="所需积分:" style="margin-left:25%">
          <span>{{goodOrderInfo.needPoint}}</span>
        </el-form-item>

        <el-button @click="doCancle()">取 消</el-button>
        <el-button type="primary" @click="createOrder()">确 定</el-button>

      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default{
  data(){
    return{
      dialogConfirmOrderVisible:false,
      //订单商品的信息
      goodOrderInfo:{
        goodId: "",
        goodName:'',
        goodImg:'',
        needPoint:99999,
      },
      customerInfo:{
        point:0.0,
        account:'',
      },
      nowTime:'',
      jwt:'',
      rules:{
        goodName: [
          { message: '商品名称', trigger: 'blur' }
        ],
        goodCode: [
          {  message: '商品编号', trigger: 'blur' }
        ],
        shelfStatus: [
          {message: '请选择上架状态', trigger: 'change' }
        ]
      },
      //查询相关
      check:{
        goodName:'',
      },
      //分页相关
      pageGoodData:{
        list:[],
        total:0,
        pageNum:1,
        pageSize:5,
        pages:1
      },
      //结算信息
      countPointMesssage:{
        address:{account:'',region:'',phone:'',receiver:'',address:''},
        pointNeed:'',
        orders:{
          orderNum:'',
          deductionAmount:'', //积分抵扣金额, 应该与pointNeed一致
          buyNum:1,
          actualPay:0,
          createTime:'',
          orderState:2,
          goodId:'',
          account:'',
        }
      }
    }
  },
  methods:{
    createOrder(){
      let jwt=localStorage.getItem("jwt");
      console.log("当前countPointMesssage");
      console.log(this.countPointMesssage);

      this.$axios.post('api/customer/countPoint',this.countPointMesssage,{headers:{"jwt":jwt}}).then(res =>{
        console.log(res.data);
        //如果订单创建成功, 返回积分商城页
        if (res.data.code == 200){
          //清零页面订单信息
          this.countPointMesssage = {
            address:{account:'',region:'',phone:'',receiver:'',address:''},
            pointNeed:'',
                orders:{
              orderNum:'',
                  deductionAmount:'', //积分抵扣金额, 应该与pointNeed一致
                  buyNum:1,
                  actualPay:0,
                  createTime:'',
                  orderState:2,
                  goodId:'',
                  account:'',
            }
          };
          this.dialogConfirmOrderVisible=false;
          //重新请求
          this.queryGood();
          this.queryPoint();
        }else{
          //生成订单报错!
          this.$message({
            type: 'info',
            message: res.data.msg
          });
        }
      });
    },

    goOrderPage(row){
      //打开确认订单页面
      //1. 获取当前商品信息
      this.goodOrderInfo.goodId = row.id;
      this.goodOrderInfo.goodName = row.goodName;
      this.goodOrderInfo.goodImg = row.goodImg;
      this.goodOrderInfo.needPoint = row.pointPrice;
      //2. 将订单信息预导入

      this.countPointMesssage.pointNeed =  this.goodOrderInfo.needPoint;
      let time = parseInt(new Date().getTime() / 1000) + '';
      let s1 = time.substring(3,8); //时间戳
      this.countPointMesssage.orders.orderNum = "HG"+s1;
      this.countPointMesssage.orders.deductionAmount = this.goodOrderInfo.needPoint;
      this.getNowDate();
      this.countPointMesssage.orders.createTime=this.nowTime;
      this.countPointMesssage.orders.goodId=this.goodOrderInfo.goodId;

      //3. 打开确认页
      this.dialogConfirmOrderVisible = true;
    },
    getNowDate() {
      var date = new Date();
      var sign2 = ":";
      var year = date.getFullYear() // 年
      var month = date.getMonth() + 1; // 月
      var day = date.getDate(); // 日
      var hour = date.getHours(); // 时
      var minutes = date.getMinutes(); // 分
      var seconds = date.getSeconds() //秒
      var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
      var week = weekArr[date.getDay()];
      // 给一位数的数据前面加 “0”
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (day >= 0 && day <= 9) {
        day = "0" + day;
      }
      if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
      }
      if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
      }
      if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
      }
      this.nowTime =  year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
    },
    doCancle(){
      history.go(-1);
    },
    queryGood(){
      let jwt=localStorage.getItem("jwt");
      this.$axios
          .get('api/goods/pointGoodsList?goodName='+this.check.goodName+'&pageNum='
          +this.pageGoodData.pageNum+'&pageSize='+this.pageGoodData.pageSize,{headers:{"jwt":jwt}})
          .then(res =>{
            console.log(res.data);
            // console.log(response.data);
            if(res.data.code==200){
              this.pageGoodData=res.data.data;
              var account = sessionStorage.getItem("account");
              this.countPointMesssage.address.account=account;
              this.countPointMesssage.orders.account=account;
              // this.$message.success(response.data.msg);
            }else if(res.data.data==406) {
              this.$message.warning(res.data.msg);
              this.$router.push("/login")
            }else {
              this.pageGoodData.list=null;
              this.pageGoodData.pages=0;
              this.pageGoodData.total=0;
              this.pageGoodData.pageNum=1;
              this.pageGoodData.pageSize=5;
              this.$message.warning(res.data.msg);
            }
          });
    },
    doReset(check){
      this.$refs[check].resetFields();
      this.pageGoodData.pageSize=5;
      this.pageGoodData.pageNum=1;
      this.queryGood();
    },
    nextPage(pageNo){
      this.pageGoodData.pageNum=pageNo;
    },
    prevPage(pageNo){
      this.pageGoodData.pageNum=pageNo;
    },
    currentPage(pageNo){
      this.pageGoodData.pageNum=pageNo;
      this.queryGood();
    },
    sizeChange(size){
      this.pageGoodData.pageSize=size;
      if (this.pageGoodData.pageNum!=1){
        this.pageGoodData.pageNum=1;
      }else{
        this.queryGood();
      }
    },
    queryPoint(){
      //获取会员积分余额信息
      let account = sessionStorage.getItem("account");
      let jwt=localStorage.getItem("jwt");
      this.$axios.get('api/customer/selectByAccount?account='+account,{headers:{"jwt":jwt}}).then(res =>{
        console.log(res.data);
        this.customerInfo.point = res.data.data.point;
      });
    }
  },
  created:function (){
  this.queryPoint();
  this.queryGood();
  }
}
</script>

<style scoped>

</style>